export default class Path {
    private _list: cc.Vec2[] = [];
    private _curIndex: number = 0;
    private _isLooped: boolean = false;

    public constructor() {
        this._isLooped = false;
    }

    public createRandomPath(numPoint: number, minX: number, minY: number, maxX: number, maxY: number, isLoop: boolean = false) {
        this.clear();
        let midx = (maxX - minX) * 0.5;
        let midy = (maxY - minY) * 0.5;
        let smaller = Math.min(midx, midy);
        let spacing = 2 * Math.PI / numPoint;
        for (let i = 0; i < numPoint; i++) {
            let radiusdist = Math.random() * smaller * 0.8 + (smaller * 0.2);
            let temp = cc.v2(radiusdist, 0);
            temp.rotateSelf(i * spacing);
            // temp.x += midx; temp.y += midy;
            this._list.push(temp);
        }
        this._isLooped = isLoop;
    }

    public getCurPoint() {
        return this._list[this._curIndex];
    }

    public isFinish() {
        return this._curIndex == this._list.length - 1;
    }

    public setLoop(loop: boolean) {
        this._isLooped = loop;
    }

    public addPoint(newPoint: cc.Vec2) {
        this._list.push(newPoint);
    }

    public clear() {
        this._list.length = 0;
        this._curIndex = 0;
    }

    public getPath() {
        return this._list;
    }

    public setUpNextPoint() {
        if (++this._curIndex == this._list.length) {
            if (this._isLooped) {
                this._curIndex = 0;
            } else {
                this._curIndex = this._list.length - 1;
            }
        }
    }
}